package leetcode;

public class Code415 {
    public String addStrings(String num1, String num2) {
        return getSum(new StringBuilder(num1),new StringBuilder(num2)).toString();
    }
    private StringBuilder getSum(StringBuilder before, StringBuilder other) {
        if (before.length() < other.length()) {
            StringBuilder i = before;
            before = other;
            other = i;
        }
        StringBuilder stringBuilder = new StringBuilder();
        int sumBefore = 0;
        int a = before.length() - 1, b = other.length() - 1;
        while (b >= 0 || sumBefore != 0) {
            int otherNumber = 0;
            int beforeNumber= 0;
            if (b >= 0) {
                otherNumber = Character.getNumericValue(other.charAt(b));
            }
            if(a>=0){
                beforeNumber = Character.getNumericValue(before.charAt(a));
            }
            int now = beforeNumber + otherNumber + sumBefore;
            stringBuilder.append(String.valueOf(now % 10));
            sumBefore = now / 10;
            if (b >= 0) {
                b--;
            }
            if(a>=0){
                a--;
            }
        }
        String aaa=before.reverse().toString().substring(before.length()-1-a,before.length());
        stringBuilder.append(aaa);
        return stringBuilder.reverse();
    }

}
